package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.mapper.BlogCategoryMapper;
import com.carkeeper.mapper.BlogMapper;
import com.carkeeper.pojo.Blog;
import com.carkeeper.pojo.BlogCategory;
import com.carkeeper.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    BlogMapper blogMapper;
    @Autowired
    BlogCategoryMapper blogCategoryMapper;

    @Override
    public Boolean addBlog(Blog blog) {
        int insert = blogMapper.insert(blog);
        if (insert > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean updateBlog(Blog blog) {
        int update = blogMapper.updateById(blog);
        if (update > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Blog queryById(long id) {
        Blog blog = blogMapper.selectById(id);
        BlogCategory blogCategory = blogCategoryMapper.selectById(blog.getBlogCategoryId());
        blog.setBlogCategoryName(blogCategory.getName());
        return blog;
    }

    @Override
    public IPage<Blog> queryBlogPage(int pageNum, int pageSize, Blog blog) {
        Page<Blog> blogPage = new Page<>(pageNum, pageSize);
        IPage<Blog> blogIPage = blogMapper.selectPage(blogPage, createWrapper(blog));
        return blogIPage;
    }

    private Wrapper createWrapper(Blog blog) {
        LambdaQueryWrapper<Blog> blogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (null != blog) {
            if (!StringUtils.isEmpty(blog.getTitle())) {
                blogLambdaQueryWrapper.like(Blog::getTitle, blog.getTitle());
            }
            if (!StringUtils.isEmpty(blog.getBlogCategoryName())) {
                QueryWrapper<BlogCategory> blogCategoryQueryWrapper = new QueryWrapper<>();
                blogCategoryQueryWrapper.like("name", blog.getBlogCategoryName());
                BlogCategory blogCategory = blogCategoryMapper.selectOne(blogCategoryQueryWrapper);
                blogLambdaQueryWrapper.eq(Blog::getBlogCategoryId, blogCategory.getId());
            }
            if (!StringUtils.isEmpty(blog.getCreator())) {
                blogLambdaQueryWrapper.like(Blog::getCreator, blog.getCreator());
            }
            if (null != blog.getStartTime()) {
                blogLambdaQueryWrapper.gt(Blog::getStartTime, blog.getStartTime());
            }
            if (null != blog.getEndTime()) {
                blogLambdaQueryWrapper.lt(Blog::getEndTime, blog.getEndTime());
            }
            if (!StringUtils.isEmpty(String.valueOf(blog.getStatus()))) {
                blogLambdaQueryWrapper.eq(Blog::getStatus, blog.getStatus());
            }
        }
        return blogLambdaQueryWrapper;
    }
}
